package com.sas.web.panel.clients;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.sas.domain.Client;
import com.sas.domain.Flow;
import com.sas.model.EntityModel;
import com.sas.repository.ClientRepository;
import com.sas.utils.Formatter;
import com.sas.web.clients.ClientSinglePage;

public class ClientsListPanel extends Panel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1365630608831780829L;
	
	private ModalWindow modal1 = null;
	
	@SpringBean
	private ClientRepository clientRepo;

	@SuppressWarnings("serial")
	public ClientsListPanel(String id) {
		super(id);
		SortableDataProvider<IModel<Client>> dp = new ClientSortableDataProvider();
		dp.setSort("name", true);
		
		final DataView<IModel<Client>> dataView = createDataView(dp);
		
		add(new OrderByBorder("orderByCuilCuit", "cuilCuit", dp) {
			@Override
            protected void onSortChanged()
            {
                dataView.setCurrentPage(0);
            }
		});
		
		add(new OrderByBorder("orderByName", "name", dp) {
			@Override
            protected void onSortChanged()
            {
                dataView.setCurrentPage(0);
            }
		});
		
		add(modal1 = new ModalWindow("modal"));
        
        
        add(dataView);
        add(new PagingNavigator("navigator", dataView));
		
	}
	
	@SuppressWarnings("serial")
	private DataView<IModel<Client>> createDataView(
			SortableDataProvider<IModel<Client>> dp) {

		DataView<IModel<Client>> ret = new DataView<IModel<Client>>("clientsList", dp) {
			@Override
			protected void populateItem(Item<IModel<Client>> item) {
				Client client = item.getModelObject().getObject();
				item.add(new Label("cuilCuit", client.getCuilCuit()));
				item.add(new Label("name", client.getName()));
				item.add(new Label("description", client.getDescription()));
				item.add(new Label("address", Formatter.format(client.getAddress())));
				item.add(new Label("telephone", Formatter.format(client.getTelephone())));
				item.add(new Label("alternative", Formatter.format(client.getAlternative())));
				item.add(new Label("fax", Formatter.format(client.getFax())));
				item.add(new Label("cellphone", Formatter.format(client.getCellphone())));
				item.add(new Label("lastBuy", Formatter.getLastBuy(client.getHistory(), Flow.FlowType.SALE)));
				item.add(new AjaxLink<Void>("show") {
					@Override
					public void onClick(AjaxRequestTarget target) {
						modal1.setPageMapName("modal-1");
						modal1.setCookieName("modal-1");
						
						modal1.setPageCreator(new ModalWindow.PageCreator() {
							@SuppressWarnings("unchecked")
							public Page createPage() {
								return new ClientSinglePage((IModel<Client>) getParent().getDefaultModelObject(), modal1);
							}
						});
						
						modal1.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {
							@Override
							public void onClose(AjaxRequestTarget target) {
								setResponsePage(getPage());								
							}
						});
						
						modal1.show(target);
					}
	            });
			}
		};
		
		return ret;
	}

	@SuppressWarnings("serial")
	protected class ClientSortableDataProvider extends SortableDataProvider<IModel<Client>> {

		@Override
		public Iterator<? extends IModel<Client>> iterator(int first, int count) {
			SortParam sortParam = getSort();
			List<Client> list = clientRepo.findByProperty(sortParam.getProperty(), sortParam.isAscending());
			List<IModel<Client>> listIModel = new ArrayList<IModel<Client>>();
			for (Client c: list) {
				listIModel.add(new EntityModel<Client>(Client.class, c));
			}
			
			return listIModel.iterator();
		}

		@Override
		public IModel<IModel<Client>> model(final IModel<Client> object) {
			return new LoadableDetachableModel<IModel<Client>>() {

				@Override
				protected IModel<Client> load() {
					return object;
				}
			};
		}

		@Override
		public int size() {
			List<Client> list = clientRepo.findAll();
			return list == null ? 0 : list.size();
		}
		
	}



}

